<template>
    <div class="right">
        <!-- 第一个 -->
        <div class="one"></div>
        <!-- 第二个 -->
        <div class="two"></div>
        <!-- 第三个 -->
        <div class="three"></div>
    </div>
</template>
<script setup>
import * as echarts from "echarts";
import { onMounted } from "vue";
import $ from 'jquery'

onMounted(() => {
    let leftone
    let fn = new Promise((resolve, reject) => {
        //2.jquery中ajax也封装好了jsonp-缺点：1.只能用get方法请求,不能用post 2.还得后台配合你写回调参数
        $.ajax({
            url: 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_foreign',
            dataType: "jsonp",
            // jsonp: "callback",//传给后端用来获取jsonp回调函数名的参数名，一般默认是callback。不写也可以
            // jsonpCallback:"cb",//也可跟后端约定函数名
            success(res) {
                // console.log('ajax数据', res);
                let datalist = JSON.parse(res.data);
                // console.log(datalist);
                let left1 = datalist.countryAddConfirmRankList
                let left2 = datalist.continentStatis
                let right1 = datalist.globalDailyHistory
                let right2 = datalist.foreignList
                let right3 = datalist.importStatis
                let cont1 = datalist.globalStatis
                resolve({ left1, left2, right1, right2, right3, cont1 })
            }
        })
    })

    fn.then(res => {
        // console.log("fn", res);
        leftone = res
        one(leftone);
        two(leftone);
        three(leftone);
    })
});
// 第一个
var myChart1
function one(a) {
    myChart1 = echarts.init(document.querySelector(".one"));
    // console.log(51, a.right2);
    let zy = []
    let name = []
    a.right2.slice(0, 5).forEach(item => {
        zy.push(
            Math.round((item.heal / item.confirm) * 1000)
        )
        name.push(item.name)
    })
    let yLabel = name;
    let yData = zy;
    let bgData = [];
    for (let i in yData) {
        bgData.push(1000);
    }
    var option = {
        grid: {
            left: '5%',
            right: '5%',
            bottom: '5%',
            top: '10%',
            containLabel: true,
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'none',
            },
            formatter: function (params) {
                return (
                    params[0].name +
                    '<br/>' +
                    "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:rgba(36,207,233,0.9)'></span>" +
                    params[0].seriesName +
                    ' : ' +
                    params[0].value +
                    ' <br/>'
                );
            },
        },
        xAxis: {
            show: false,
            type: 'value',
        },
        yAxis: [
            {
                type: 'category',
                inverse: true,
                axisLabel: {
                    show: true,
                    margin: 15,
                    textStyle: {
                        color: '#fff',
                    },
                },
                splitLine: {
                    show: false,
                },
                axisTick: {
                    show: false,
                },
                axisLine: {
                    show: false,
                },
                data: yLabel,
            },
            {
                type: 'category',
                inverse: true,
                axisTick: 'none',
                axisLine: 'none',
                show: true,
                axisLabel: {
                    textStyle: {
                        color: '#2a82fd',
                        fontSize: '12',
                    },
                },
                data: yData.map(item => {
                    return (item / 1000 * 100).toFixed(0) + '%'
                }),
            },
        ],
        series: [
            {
                name: '人数',
                type: 'bar',
                zlevel: 1,
                itemStyle: {
                    normal: {
                        barBorderRadius: 30,
                        color: '#2a82fd',
                        shadowBlur: 0,
                        shadowColor: 'rgba(87,220,222,0.7)',
                    },
                },
                barWidth: 20,
                data: yData,
            },
            {
                name: '背景',
                type: 'bar',
                barWidth: 20,
                barGap: '-100%',
                data: bgData,
                itemStyle: {
                    normal: {
                        color: 'rgba(24,31,68,1)',
                        barBorderRadius: [0, 30, 30, 0],
                    },
                },
            },
        ],
    };

    myChart1.setOption(option);
    // window.addEventListener(
    //     "resize",
    //     function (e) {
    //         myChart.resize();

    //     },
    //     false
    // );
}
var myChart2
// 第二个
function two(a) {
    myChart2 = echarts.init(document.querySelector(".two"));
    let glod = []
    let x = []
    let zy = []
    a.right1.forEach(item => {
        // console.log(item);
        x.push(item.date)
        zy.push(item.all.heal)
        glod.push(item.all.dead)
    })
    let xLabel = ['3.26', '3.27', '3.28', '3.29', '3.30', '3.31']
    var option = {
        // backgroundColor: '#0e1c47',
        tooltip: {
            trigger: 'axis',
            backgroundColor: 'transparent',
            axisPointer: {
                lineStyle: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0,
                            color: 'rgba(126,199,255,0)' // 0% 处的颜色
                        }, {
                            offset: 0.5,
                            color: 'rgba(126,199,255,1)' // 100% 处的颜色
                        }, {
                            offset: 1,
                            color: 'rgba(126,199,255,0)' // 100% 处的颜色
                        }],
                        global: false // 缺省为 false
                    }
                },
            },
            formatter: (p) => {
                let dom = `<div style="width: 79px;
	height: 50px;;color:#fff;position: relative;">
        <svg style="position: absolute;top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);" class="svg" xmlns="http://www.w3.org/2000/svg" width="100" height="71" viewBox="0 0 84 55">
      <defs>
        <style>
          .cls-1 {
            fill: #07172c;
            fill-opacity: 0.8;
            stroke: #a7d8ff;
            stroke-linejoin: round;
            stroke-opacity: 0.2;
            stroke-width: 1px;
            fill-rule: evenodd;
          }

        </style>
      </defs>
      <path id="矩形_419" data-name="矩形 419" class="cls-1" d="M266,595h74v50H266V624.046L261,620l5-3.984V595Z"
        transform="translate(-258.5 -592.5)" />
    </svg>
        <div style="padding: 4px 8px 4px 14px;display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;position: relative;z-index: 1;">
            <div style="margin-bottom: 4px;width:100%;display:${p[0] ? 'flex' : 'none'};justify-content:space-between;align-items:center;">
                <span style="font-size:14px;color:#7ec7ff;">${p[0] ? p[0].seriesName : ''}</span>
                <span style="font-size:14px;color:#fff;">${p[0] ? p[0].data : ''}</span>
            </div>
            <div style="width:100%;height:100%;display:${p[1] ? 'flex' : 'none'};justify-content:space-between;align-items:center;">
                <span style="font-size:14px;color:#7ec7ff;">${p[1] ? p[1].seriesName : ''}</span>
                <span style="font-size:14px;color:#fff;">${p[1] ? p[1].data : ''}</span>
            </div>
        </div>
    </div>`
                return dom
            }
        },
        legend: {
            align: "left",
            right: '10%',
            top: '10%',
            left: '40%',
            type: 'plain',
            textStyle: {
                color: '#7ec7ff',
                fontSize: 10
            },
            // icon:'rect',
            itemGap: 25,
            itemWidth: 18,
            icon: 'path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z',

            data: [
                {
                    name: '治愈率'
                },
                {
                    name: '死亡率'
                }
            ]
        },
        grid: {
            top: '8%',
            left: '25%',
            right: '10%',
            bottom: '25%',
            // containLabel: true
        },
        xAxis: [{
            type: 'category',
            boundaryGap: false,
            axisLine: { //坐标轴轴线相关设置。数学上的x轴
                show: true,
                lineStyle: {
                    color: '#233653'
                },
            },
            axisLabel: { //坐标轴刻度标签的相关设置
                textStyle: {
                    color: '#7ec7ff',
                    padding: 16,
                    fontSize: 12
                },
                formatter: function (data) {
                    return data
                }
            },
            splitLine: {
                show: true,
                lineStyle: {
                    color: '#192a44'
                },
            },
            axisTick: {
                show: false,
            },
            data: x
        }],
        yAxis: [{
            // name: '人数',
            nameTextStyle: {
                color: "#7ec7ff",
                fontSize: 16,
                padding: 10
            },
            min: 0,
            splitLine: {
                show: true,
                lineStyle: {
                    color: '#192a44'
                },
            },
            axisLine: {
                show: true,
                lineStyle: {
                    color: "#233653"
                }

            },
            axisLabel: {
                show: true,
                textStyle: {
                    color: '#7ec7ff',
                    padding: 16
                },
                formatter: function (value) {
                    if (value === 0) {
                        return value
                    }
                    return value
                }
            },
            axisTick: {
                show: false,
            },
        }],
        series: [{
            name: '治愈率',
            type: 'line',
            symbol: 'circle', // 默认是空心圆（中间是白色的），改成实心圆
            showAllSymbol: true,
            symbolSize: 0,
            smooth: true,
            lineStyle: {
                normal: {
                    width: 5,
                    color: "rgba(25,163,223,1)", // 线条颜色
                },
                borderColor: 'rgba(0,0,0,.4)',
            },
            itemStyle: {
                color: "rgba(25,163,223,1)",
                borderColor: "#646ace",
                borderWidth: 2

            },
            tooltip: {
                show: true
            },
            areaStyle: { //区域填充样式
                normal: {
                    //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: "rgba(25,163,223,.3)"


                    },
                    {
                        offset: 1,
                        color: "rgba(25,163,223, 0)"
                    }
                    ], false),
                    shadowColor: 'rgba(25,163,223, 0.5)', //阴影颜色
                    shadowBlur: 20 //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
                }
            },
            data: zy
        }, {
            name: '死亡率',
            type: 'line',
            symbol: 'circle', // 默认是空心圆（中间是白色的），改成实心圆
            showAllSymbol: true,
            symbolSize: 0,
            smooth: true,
            lineStyle: {
                normal: {
                    width: 5,
                    color: "rgba(10,219,250,1)", // 线条颜色
                },
                borderColor: 'rgba(0,0,0,.4)',
            },
            itemStyle: {
                color: "rgba(10,219,250,1)",
                borderColor: "#646ace",
                borderWidth: 2

            },
            tooltip: {
                show: true
            },
            areaStyle: { //区域填充样式
                normal: {
                    //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: "rgba(10,219,250,.3)"
                    },
                    {
                        offset: 1,
                        color: "rgba(10,219,250, 0)"
                    }
                    ], false),
                    shadowColor: 'rgba(10,219,250, 0.5)', //阴影颜色
                    shadowBlur: 20 //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
                }
            },
            data: glod
        }]
    };
    let len = 0
    setInterval(() => {
        if (len === xLabel.length) {
            len = 0
        }
        myChart2.dispatchAction({
            type: 'showTip',
            seriesIndex: 0,
            dataIndex: len,
        })
        len++
    }, 1000)
    myChart2.setOption(option);
    // window.addEventListener(
    //     "resize",
    //     function (e) {
    //         myChart.resize();

    //     },
    //     false
    // );
}
var myChart6
// 第三个
function three(a) {
    myChart6 = echarts.init(document.querySelector(".three"));
    let add = []
    let val = []
    a.right3.TopList.forEach(item => {
        // console.log(451, item);
        val.push(item.importedCase)
        add.push(item.province)
        // console.log(val);
    })
    var colorList = [{
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 1,
        colorStops: [
            {
                offset: 1,
                color: '#0076ff' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 1,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
            {
                offset: 1,
                color: '#69e1b0' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 1,
        y: 0,
        x2: 0,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#ff9591' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 0,
        y: 1,
        x2: 0,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#ffae8b' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 1,
        y: 1,
        x2: 1,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#00a5ff' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#aefdca' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#37d8ff' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#1facff' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#ffae8b' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 0,
        colorStops: [
            {
                offset: 1,
                color: '#00a5ff' // 100% 处的颜色
            }
        ],
        globalCoord: false // 缺省为 false
    },
    ]
    var colorLine = ['#0076ff', '#69e1b0', '#ff9591', '#ffae8b', '#00a5ff', '#aefdca', '#37d8ff', '#1facff', '#ffae8b', '#00a5ff']

    function getRich() {
        let result = {}
        colorLine.forEach((v, i) => {
            result[`hr${i}`] = {
                backgroundColor: colorLine[i],
                borderRadius: 3,
                width: 3,
                height: 3,
                padding: [3, 3, 0, -12]
            }
            result[`a${i}`] = {
                padding: [-11, 6, -20, 6],
                color: colorLine[i],
                backgroundColor: 'transparent',
                fontSize: 12
            }
        })
        return result
    }
    var data = [{
        name: add[0],
        value: val[0]
    }, //亚洲
    {
        name: add[1],
        value: val[1]
    },
    {
        name: add[2],
        value: val[2]
    },
    {
        name: add[3],
        value: val[3]
    },
    {
        name: add[4],
        value: val[4]
    },
    {
        name: add[5],
        value: val[5]
    },
    {
        name: add[6],
        value: val[6]
    },
    {
        name: add[7],
        value: val[7]
    },
    {
        name: add[8],
        value: val[8]
    },
    {
        name: add[9],
        value: val[9]
    },
    ];

    var option = {
        series: [{
            type: 'pie',
            radius: '60%',
            center: ['50%', '50%'],
            clockwise: true,
            avoidLabelOverlap: true,
            label: {
                show: true,
                position: 'outside',
                formatter: function (params) {
                    const name = params.name
                    const percent = params.percent + '%'
                    const index = params.dataIndex
                    return [`{a${index}|${name}：${percent}}`, `{hr${index}|}`].join('\n')
                },
                rich: getRich()
            },
            itemStyle: {
                normal: {
                    color: function (params) {
                        return colorList[params.dataIndex]
                    }
                }
            },
            data,
            roseType: 'radius'
        }]
    };
    myChart6.setOption(option);

}
window.addEventListener(
    "resize",
    function (e) {
        myChart6.resize();
        myChart1.resize();
        myChart2.resize();
    },
    false
);
</script>
<style lang="scss" scoped>
.right {
    width: 520px;
    height: 980px;
    display: flex;
    flex-wrap: wrap;

    .one {
        width: 100%;
        height: 33%;
        // padding: 20px;
    }

    .two {
        height: 33%;
        width: 100%;
    }

    .three {
        width: 100%;
        height: 33%;
    }
}
</style>